<template>
  <div>
    <div class="layui-container fly-marginTop">
      <div class="fly-panel fly-panel-user" pad20>
        <div class="layui-tab layui-tab-brief" lay-filter="user">
          <ul class="layui-tab-title">
            <li class="layui-this">登入</li>
            <li><router-link to="/register">注册</router-link></li>
          </ul>
          <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
            <div class="layui-tab-item layui-show">
              <div class="layui-form layui-form-pane">
                  <div class="layui-form-item">
                    <label for="L_email" class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                      <input v-model="email" type="text" id="L_email" name="email" required lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label for="L_pass" class="layui-form-label">密码</label>
                    <div class="layui-input-inline">
                      <input v-model="password" type="password" id="L_pass" name="pass" required lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label for="L_vercode" class="layui-form-label">人类验证</label>
                    <div class="layui-input-inline">
                      <input v-model="code" type="text" id="L_vercode" name="vercode" required lay-verify="required" placeholder="请回答后面的问题" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">
                      <span style="color: #c00;">{{one}} + {{two}} = ?</span>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <button @click="submitForm" class="layui-btn" lay-filter="*" lay-submit>立即登录</button>
                    <span style="padding-left:20px;">
                  <a>忘记密码？</a>
                </span>
                  </div>

              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {message} from "@/element/message";
import {login} from "@/api/login";

export default {
  name: 'RegisterLogin',
  data () {
    return {
      email: '',
      password: '',
      code: '',
      one: Math.floor(Math.random()*10),
      two: Math.floor(Math.random()*10)
    }
  },
  methods: {
    submitForm: function () {
      if (this.code != this.one + this.two) {
        message('人类验证错误', 'warning')
        return
      }

      login(this.email, this.password).then(this.loginSucc)
    },
    loginSucc: function (res) {
      if (res) {
        message('登录成功!')
        localStorage.setItem("user", JSON.stringify(res.data.data));
        this.$store.state.user = res.data.data
        this.$router.push({
          path:'/'
        })
      }
    }
  }

}
</script>
